<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文字转图片</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f2f5;
        }

        .container {
            background-color: #fff;
            border-radius: 16px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 30px;
        }

        h1 {
            margin-bottom: 20px;
            color: #333;
            font-size: 24px;
        }

        .content {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
        }

        #ttsTextarea {
            width: 100%;
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            resize: vertical;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }

        #ttsTextarea:focus {
            border-color: #007bff;
        }

        #submitBtn {
            padding: 12px 24px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        #submitBtn:hover {
            background-color: #0056b3;
        }

        #submitBtn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        #ttsResponse {
            margin-top: 20px;
            text-align: center;
            width: 100%;
        }

        .loading, .completed, .error {
            margin-top: 10px;
            font-size: 16px;
        }

        .loading {
            color: #007bff;
        }

        .completed {
            color: green;
        }

        .error {
            color: red;
        }

        img {
            max-width: 100%;
            height: auto;
            border: 1px solid #ddd;
            border-radius: 8px;
            margin-top: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
<div class="container">
    <h1>文字转图片</h1>
    <div class="content">
        <div id="ttsResponse"></div>
        <form id="ttsForm">
            <textarea name="input" id="ttsTextarea" cols="30" rows="5" placeholder="请输入文字描述" required></textarea>
            <button type="submit" id="submitBtn">生成图片</button>
        </form>
    </div>
</div>

<script src="./jquery.min.js"></script>
<script>
    $(document).ready(function() {
        // 监听表单提交事件
        $("#ttsForm").submit(function(event) {
            event.preventDefault();
            if ($("#ttsTextarea").val().length < 5) {
                alert("文本太短了");
            } else {
                $("#submitBtn").prop('disabled', true);
                $("#ttsResponse").html('<div class="loading">正在生成...</div>');
                $.get("/ai/image/" + encodeURIComponent($("#ttsTextarea").val()), {}, function(resp) {
                    $("#ttsResponse").html(`
                        <img src="${resp}">
                        <div class="completed">生成完成</div>
                    `);
                }).fail(function() {
                    $("#ttsResponse").html('<div class="error">生成失败，请重试</div>');
                }).always(function() {
                    $("#submitBtn").prop('disabled', false);
                });
            }
        });

        // 监听 Enter 键按下事件
        $("#ttsTextarea").keydown(function(event) {
            if (event.key === "Enter" && !event.shiftKey) {
                event.preventDefault();
                $("#ttsForm").submit();
            }
        });
    });
</script>

</body>
</html>
